package com.dq.controller.app.user;

import com.alibaba.fastjson.JSONObject;
import com.dq.controller.in.user.WxUserQuery;
import com.dq.controller.in.user.request.WxUserRequest;
import com.dq.domain.service.ServiceProject;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.centre.UserCard;
import com.dq.domain.sys.user.user.centre.UserCardServiceProjectCentre;
import com.dq.domain.sys.user.user.centre.UserLableCentre;
import com.dq.exception.customException.AdminException;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.dq.vo.baseVo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */

@RestController
@RequestMapping("/app/wxuser/")
@Api(description = "顾客档案管理")
public class AppWxUserController extends AllService {


    @PostMapping("list")
    @ApiOperation("顾客档案列表")
    public Vo customerList(@RequestBody WxUserQuery wxUserQuery)throws AdminException {
        List<WxUser> list = wxUserService.wxUserList(wxUserQuery);

        List<WxUser.FrontWxUser> toList = new ArrayList<>();
        for (WxUser wxUser : list) {
            WxUser.FrontWxUser frontWxUser = wxUser.toFrontWxUser();

            frontWxUser.setLableStrList(wxUserService.getWxUserLable(wxUser));

            toList.add(frontWxUser);
        }

        return new ListDataVo(toList, wxUserService.wxUserCount(wxUserQuery));
    }
    @ApiOperation("查看顾客详细信息, 包括会员卡集合")
    @GetMapping("lookUser/{wxUserId}")
    public Vo lookUser(@PathVariable @ApiParam("顾客ID")Long wxUserId)throws AdminException{
        WxUser wxUser = dao.get(WxUser.class, wxUserId);
        WxUser.FrontWxUser frontWxUser = wxUser.toFrontWxUser();
        frontWxUser.setLableStrList(wxUserService.getWxUserLable(wxUser));
        frontWxUser.setUserCardList(wxUserService.getWxUserCardList(wxUser, null));

        return new Vo(frontWxUser);
    }

    /**
     * 添加顾客记录, 就是修改顾客信息
     */
    @PostMapping("addRecord")
    @ApiOperation("添加顾客记录")
    public Vo addRecord(@Valid @RequestBody WxUserRequest wxUserRequest)throws AdminException {
        WxUser wxUser = new WxUser();
        wxUser.setId(wxUserRequest.getWxUserId());
        wxUser.setLableIdList(wxUserRequest.getLableIdList());
        wxUser.setRemark(wxUserRequest.getRemark());

        wxUserService.update(wxUser, false);
        return new Vo(HttpStatus.OK);
    }

    @GetMapping("userCardList/{wxuserId}/{orgId}")
    @ApiOperation("单个门店, 下顾客会员卡列表")
    public Vo userCardList(@PathVariable @ApiParam("顾客ID") Long wxuserId, @PathVariable @ApiParam("当前登录门店ID") Long orgId)throws AdminException {
        WxUser wxUser = dao.get(WxUser.class, wxuserId);
        if(wxUser == null) {
            throw new AdminException("获取顾客异常");
        }

        return new ListDataVo(wxUserService.getWxUserCardList(wxUser, orgId));
    }

}
